package com.example.application.task;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeatherTask1 implements Runnable{
    private final String TAG="WeatherTask1";
    private Handler handler;
    public Handler getHandler() {
        return handler;
    }
    public void setHandler(Handler handler) {
        this.handler = handler;
    }
    @Override
    public void run() {
        Log.i(TAG, "run:run()....");
        List<String> relist1=new ArrayList<String>();
        List<String> relist2=new ArrayList<String>();
        try {
            Document doc = Jsoup.connect("https://tianqi.so.com/weather/101270101").get();
                Elements cur = doc.getElementsByClass("cur-weather g-fl");
                Elements forc = doc.getElementsByClass("weather-forcast-wrap");
                Elements cur21 = doc.getElementsByClass("cur-quality-wrap g-fl").first().getElementsByClass("mh-pm25-num js-pm25-num");
                Elements cur22 = doc.getElementsByClass("cur-quality-wrap g-fl").first().getElementsByClass("mh-pm25-txt");
                Log.i(TAG, "run: "+cur22);
                Elements cur_all=cur.first().getElementsByTag("p");
                Element cur_temp=cur_all.first();
                Element cur_dir=cur_all.get(2);
                Element cur_str=cur_all.get(3);
                String temp = cur_temp.text().split("[ ：]")[2];
                String con = cur_temp.text().split("[ ：]")[1];
                String dir =cur_dir.text().split("[ ：]")[1];
                String str =cur_str.text().split("[ ：]")[1];
                relist1.add(con);relist1.add(temp);relist1.add(dir+str);relist1.add(cur21.text()+" "+cur22.text());
                Log.i(TAG, "run: "+cur_all);
            for(int i=0;i<7;i++){
                String a = forc.first().getElementsByTag("li").get(i).getElementsByTag("div").text();
                String a1[] = a.split(" {1,}");
                for(int j=0;j<a1.length;j++){
                    relist2.add(a1[j]);
                }
                Log.i(TAG, "run: aaa"+a);
            }
            Log.i(TAG, "run: aaa"+relist2);
        }catch (IOException e) {
            e.printStackTrace();
            Log.i(TAG, "run: "+e);
        }
        Message msg = handler.obtainMessage(5);
        Message msg2 = handler.obtainMessage(6,relist2);
        msg.obj = relist1;
        handler.sendMessage(msg2);
        handler.sendMessage(msg);
    }
}
